package com.soap.desensitize.factory.encry.irreversible;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import com.soap.desensitize.exception.factory.FactoryException;
import com.soap.desensitize.exception.factory.StrategyFactoryException;
import com.soap.desensitize.factory.encry.EStrategy;
import com.soap.desensitize.strategy.irreversible.*;

/**
 * @desc 不可逆加密策略的工厂
 */
public class IrreverSibleFactory {
    /**
     * @desc 根据Class 生产策略
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractIRreversibleEncry create(Class<? extends AbstractIRreversibleEncry> cla) throws InstantiationException, IllegalAccessException {
        return cla.newInstance();
    }

    /**
     * @desc 根据类路径生产策略
     * @param classStr
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractIRreversibleEncry create(String classStr) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        switch (classStr){
            case "com.rj.bd.strategy.irreversible.Md5Strategy":
                return Md5Strategy.getInstance();
            case "com.rj.bd.strategy.irreversible.EmailStrategy":
                return EmailStrategy.getInstance();
            case "com.rj.bd.strategy.irreversible.HmacStrategy":
                return HmacStrategy.getInstance();
            case "com.rj.bd.strategy.irreversible.IdCardStrategy":
                return IdCardStrategy.getInstance();
            case "com.rj.bd.strategy.irreversible.PhoneStrtegy":
                return PhoneStrtegy.getInstance();
            case "com.rj.bd.strategy.irreversible.Sha1Strategy":
                return Sha1Strategy.getInstance();
            case "com.rj.bd.strategy.irreversible.Sha224Strategy":
                return Sha224Strategy.getInstance();
            case "com.rj.bd.strategy.irreversible.Sha256Strategy":
                return Sha256Strategy.getInstance();
            case "com.rj.bd.strategy.irreversible.SubPhoneNumberStrtegy":
                return SubPhoneNumberStrtegy.getInstance();
        }
        throw new StrategyFactoryException("没有找到这个策略 "+classStr);
    }

    /**
     * @desc 根据类路径生产策略
     * @param eStrategy 策略类枚举
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractIRreversibleEncry create(EStrategy eStrategy) throws FactoryException {
        try {
            return create(eStrategy.getClassName());
        } catch (InstantiationException e) {
            throw new FactoryException("实例化异常 InstantiationException");
        } catch (IllegalAccessException e) {
            throw new FactoryException("非法访问异常 IllegalAccessException");
        } catch (StrategyFactoryException e) {
            throw new RuntimeException(e);
        }
    }

}
